package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.PageParm;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    @GetMapping("/findAll")
    public Result list(CheckGroup checkGroup){
        log.info("查询检查组");
        List<CheckGroup> list=checkGroupService.list(checkGroup);

        return Result.success(list);
    }

    @PostMapping("/add")
    public Result insert(@RequestParam("checkitemIds") String checkitemIds,
                         @RequestBody CheckGroup checkGroup){
        log.info("新增检查组:{}",checkitemIds);
        String[] idList=checkitemIds.split(",");
        for (String id:idList){
            checkGroup.setId(Integer.parseInt(id));
            checkGroupService.insert(checkGroup);

        }
        return Result.success();
    }


    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam("id") Integer id){
        log.info("根据id删除检查组");
        checkGroupService.deleteById(id);
        return Result.success();
    }

    /**
     * 编辑检查组
     * @param checkitemIds
     * @param checkGroup
     * @return
     */
    @PostMapping("/edit")
    public Result update(@RequestParam("checkitemIds") String checkitemIds,
                         @RequestBody CheckGroup checkGroup){
        log.info("编辑检查组：{}",checkGroup);
        String[] idList=checkitemIds.split(",");
        checkGroupService.edit(checkGroup);

        return Result.success();
    }


    /**
     * 分页查询
     * @param pageParm
     * @return
     */
    @PostMapping("/findPage")
    public PageResult page(@RequestBody PageParm pageParm) {
        log.info("分页查询:{},{}",pageParm.getCurrentPage(),pageParm.getPageSize());
        PageResult page = checkGroupService.page(pageParm.getCurrentPage(),pageParm.getPageSize());
        return page;
    }

    /**
     * 根据id查询检查项
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("id")@RequestBody Integer id){
        log.info("根据id查询检查项:{}",id);
        CheckGroup checkGroup=checkGroupService.findById(id);
        return Result.success(checkGroup);
    }

    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam("setmealId") @RequestBody Integer id){
        log.info("根据groupid查询itemid表");
        List<CheckGroup> list=checkGroupService.getItemById(id);
        return Result.success(list);
    }




}
